﻿using Dapper;
using OMG.Fill.Vegetable.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace OMG.Fill.Vegetable.Service
{
    /// <summary>
    /// 表3生产情况调查表T_VegProSurvey通用操作类
    /// </summary>
    public class VegProSurveys
    {
        private IDbConnection _conn;
        public IDbConnection dbCon
        {
            get
            {
                return _conn = ConnectFactory.GetOpenConnection();
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        public virtual ResResult<int> AddInfo(VegProSurveyEntity model) {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"INSERT INTO dbo.T_VegProSurvey";
            strSql += "(";
            strSql += "Id,";
            strSql += "ReportId,";
            strSql += "SortCode,";
            strSql += "SortName,";
            strSql += "ItemCode,";
            strSql += "ItemName,";
            strSql += "LD_Area,";
            strSql += "LD_Product,";
            strSql += "XP_Area,";
            strSql += "XP_Product,";
            strSql += "DZP_Area,";
            strSql += "DZP_Product,";
            strSql += "WS_Area,";
            strSql += "WS_Product,";
            strSql += "Remark)";
            strSql += " values(";
            strSql += "@Id,";
            strSql += "@ReportId,";
            strSql += "@SortCode,";
            strSql += "@SortName,";
            strSql += "@ItemCode,";
            strSql += "@ItemName,";
            strSql += "@LD_Area,";
            strSql += "@LD_Product,";
            strSql += "@XP_Area,";
            strSql += "@XP_Product,";
            strSql += "@DZP_Area,";
            strSql += "@DZP_Product,";
            strSql += "@WS_Area,";
            strSql += "@WS_Product,";
            strSql += "@Remark)";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    model.Id= CustomFun.GetGuid();
                    int result = dbCon.Execute(strSql, model);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "添加成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "添加失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("新增表3生产情况调查表T_VegProSurvey对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        public virtual ResResult<int> UpdateInfo(VegProSurveyEntity model)
        {
            ResResult<int> obj = new ResResult<int>();
            ReportServices reportServices = new ReportServices();
            string strSql = @"UPDATE dbo.T_VegProSurvey set ";
            strSql += "ReportId = @ReportId,";
            strSql += "SortCode = @SortCode,";
            strSql += "SortName = @SortName,";
            strSql += "ItemCode = @ItemCode,";
            strSql += "ItemName = @ItemName,";
            strSql += "LD_Area = @LD_Area,";
            strSql += "LD_Product = @LD_Product,";
            strSql += "XP_Area = @XP_Area,";
            strSql += "XP_Product = @XP_Product,";
            strSql += "DZP_Area = @DZP_Area,";
            strSql += "DZP_Product = @DZP_Product,";
            strSql += "WS_Area = @WS_Area,";
            strSql += "WS_Product = @WS_Product,";
            strSql += "Remark = @Remark";
            strSql += " where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    int result = dbCon.Execute(strSql, model);
                    int resultReport = reportServices.UpStatus(model.ReportId, "2", "").status; //更新当前报表状态为已填报
                    if (result > 0 && resultReport == 1)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "数据更新成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "更新失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表3生产情况调查表T_VegProSurvey对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public virtual ResResult<int> DeleteInfo(string id)
        {
            ResResult<int> obj = new ResResult<int>();
            string strSql = @"delete from dbo.T_VegProSurvey where Id = @Id";
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, new { Id = id });
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.model = result;
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("删除表3生产情况调查表T_VegProSurvey对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        public virtual ResResult<int> DeleteBatch(string[] ids)
        {
            ResResult<int> obj = new ResResult<int>();
            string strIds = string.Join(",", ids.Select(x => x.ToString()).ToArray());
            string strSql = @"delete from dbo.T_VegProSurvey where Id in @ids";
            var dynamicSqlParam = new DynamicParameters();
            dynamicSqlParam.Add("ids", ids.ToArray(), null, null, null);
            int result;
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    result = dbCon.Execute(strSql, dynamicSqlParam);
                    if (result > 0)
                    {
                        obj.model = result;
                        obj.status = DefaultKey.SuccessResult;
                        obj.message = "删除成功！";
                    }
                    else
                    {
                        obj.status = DefaultKey.InfoResult;
                        obj.message = "删除失败！数量为:" + result;
                    }
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog("获取数据字典表批量删除表3生产情况调查表T_VegProSurvey表失败", ex);
                obj.message = ex.ToString().Trim();
                obj.status = DefaultKey.ErrorResult;
                return obj;
            }
        }
        /// <summary>
        /// 获取指定对象
        /// </summary>
        public virtual ResResult<VegProSurveyEntity> GetModel(string id) {
            ResResult<VegProSurveyEntity> obj = new ResResult<VegProSurveyEntity>();
            string strSql = @"select * from dbo.T_VegProSurvey where Id = @Id";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegProSurveyEntity>(strSql, new { Id = id }).FirstOrDefault();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("获取表3生产情况调查表T_VegProSurvey对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <returns></returns>
        public virtual ResResult<List<VegProSurveyEntity>> GetList()
        {
            ResResult<List<VegProSurveyEntity>> obj = new ResResult<List<VegProSurveyEntity>>();
            string strSql = @"select * from dbo.T_VegProSurvey";
            try
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    obj.model = dbCon.Query<VegProSurveyEntity>(strSql).ToList();
                    obj.status = DefaultKey.SuccessResult;
                    obj.message = "获取成功！";
                    dbCon.Dispose();//数据库连接释放
                    return obj;
                }
            }
            catch (Exception ex)
            {
                obj.status = DefaultKey.ErrorResult;
                obj.message = ex.ToString().Trim();
                LogHelper.ErrorLog("更新表3生产情况调查表T_VegProSurvey对象信息失败", ex);
                return obj;
            }
        }
        /// <summary>
        /// 获取三级节点信息----获取种类信息
        /// </summary>
        /// <returns></returns>
        public ResResult<List<VegProSurveyEntity>> GetItemList(VegProSurveyEntity model)
        {
            ResResult<List<VegProSurveyEntity>> result = new ResResult<List<VegProSurveyEntity>>();
            if (!string.IsNullOrEmpty(model.ReportId) && !string.IsNullOrEmpty(model.SortCode))
            {
                try
                {
                    List<VegProSurveyEntity> listTemp = new List<VegProSurveyEntity>();
                    var dynamicSqlParam = new DynamicParameters();
                    dynamicSqlParam.Add("",model.SortCode);
                    string sqlStr = "select * from [T_VegProSurvey] where ReportId='" + model.ReportId + "' and SortCode like '" + model.SortCode + "%'";
                    listTemp = dbCon.Query<VegProSurveyEntity>(sqlStr).ToList();
                    if (listTemp.Count != 0)
                    {
                        result.model = listTemp;
                        result.status = DefaultKey.SuccessResult;
                        result.message = "获取品种对应的种类成功";
                    }
                    else
                    {
                        result.status = DefaultKey.ErrorResult;
                        result.message = "当前选项卡对应的数据不存在";
                    }
                }
                catch (Exception ex)
                {
                    result.status = DefaultKey.ErrorResult;
                    result.message = "程序异常，请与管理员联系。错误信息:" + ex.ToString();
                }
            }
            return result;
        }
        /// <summary>
        /// 蔬菜生产情况调查表----数据填报(数据更新)
        /// </summary>
        /// <returns></returns>
        public ResResult<VegProSurveyEntity> UpdateModelList(List<VegProSurveyEntity> list)
        {
            ResResult<VegProSurveyEntity> result = new ResResult<VegProSurveyEntity>();
            string strSql = @"UPDATE dbo.T_VegProSurvey set ";
            strSql += "ReportId = @ReportId,";
            strSql += "SortCode = @SortCode,";
            strSql += "SortName = @SortName,";
            strSql += "ItemCode = @ItemCode,";
            strSql += "ItemName = @ItemName,";
            strSql += "LD_Area = @LD_Area,";
            strSql += "LD_Product = @LD_Product,";
            strSql += "XP_Area = @XP_Area,";
            strSql += "XP_Product = @XP_Product,";
            strSql += "DZP_Area = @DZP_Area,";
            strSql += "DZP_Product = @DZP_Product,";
            strSql += "WS_Area = @WS_Area,";
            strSql += "WS_Product = @WS_Product,";
            strSql += "Remark = @Remark";
            strSql += " where Id = @Id";
            if (list.Count != 0)
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    using (var trans = dbCon.BeginTransaction())
                    {
                        try
                        {
                            dbCon.Execute(strSql, list, trans);
                            result.message = "更新蔬菜生产情况数据成功。";
                            result.status = DefaultKey.SuccessResult;
                            trans.Commit();
                        }
                        catch (Exception ex)
                        {
                            result.message = "更新蔬菜生产情况异常,异常信息:" + ex.ToString().Trim();
                            result.status = DefaultKey.ErrorResult;
                            LogHelper.WriteLog("更新蔬菜生产情况异常,异常信息:" + ex.ToString());
                            trans.Rollback();
                        }
                    }
                }
            }
            return result;
        }
    }
}